from ncatbot.core import BotClient, GroupMessage, PrivateMessage
from ncatbot.utils.config import config
from ncatbot.utils.logger import get_log
from ncatbot.core.element import (
    MessageChain,  # 消息链，用于组合多个消息元素
    Text,          # 文本消息
    Reply,         # 回复消息
    At,            # @某人
    AtAll,         # @全体成员
    Dice,          # 骰子
    Face,          # QQ表情
    Image,         # 图片
    Json,          # JSON消息
    Music,         # 音乐分享 (网易云, QQ 音乐等)
    CustomMusic,   # 自定义音乐分享
    Record,        # 语音
    Rps,           # 猜拳
    Video,         # 视频
    File,          # 文件
)
import jmcomic
import os


_log = get_log()

config.set_bot_uin("3926601293")  # 设置 bot qq 号 (必填)
config.set_root("123456")  # 设置 bot 超级管理员账号 (建议填写)
config.set_ws_uri("ws://localhost:8282")  # 设置 napcat websocket server 地址
config.set_token("napcat")  # 设置 token (napcat 服务器的 token)

bot = BotClient()


@bot.group_event()
async def on_group_message(msg: GroupMessage):
    _log.info(msg)
    if msg.raw_message == "测试":
        await msg.reply(text="NcatBot 测试成功喵~")

@bot.group_event(['text'])
async def on_group_file(msg: GroupMessage):
    _log.info(msg)
    if msg.raw_message.lower().startswith('jm'):
        try:
            jmcode = int(msg.raw_message[2:])
        except Exception as e:
            await msg.reply('车牌号有误')

        file_path = f'/home/ubuntu/npcat/pdfs/{jmcode}.pdf'
        root_dir = '/home/ubuntu/npcat/pdfs'

        for entry in os.listdir(root_dir):
            full_path = os.path.join(root_dir, entry)
            if os.path.isfile(full_path) and entry == file_path:
                message = MessageChain([
                    File(file_path)
                ])
                await msg.reply(rtf=message)

        option = jmcomic.create_option_by_file('option.yml')
        # 使用option对象来下载本子
        try:    
            jmcomic.download_album(jmcode, option)
        except Exception as e:
            await msg.reply('下载出问题了')

        message = MessageChain([
                    File(file_path)
                ])
        await msg.reply(rtf=message)
        


@bot.private_event()
async def on_private_message(msg: PrivateMessage):
    _log.info(msg)
    if msg.raw_message == "测试":
        await bot.api.post_private_msg(msg.user_id, text="NcatBot 测试成功喵~")

@bot.private_event()
async def on_private_file_message(msg: PrivateMessage):
    _log.info(msg)
    if msg.raw_message == "文件测试":
        message = MessageChain([
            File('home/ubuntu/npcat/test1.txt')
        ])
        await bot.api.post_private_msg(msg.user_id, rtf=message)

@bot.private_event(['text'])
async def on_private_file(msg: GroupMessage):
    _log.info(msg)
    if msg.raw_message.lower().startswith('jm'):
        try:
            jmcode = int(msg.raw_message[2:])
        except Exception as e:
            await msg.reply('车牌号有误')

        file_path = f'/home/ubuntu/npcat/pdfs/{jmcode}.pdf'
        root_dir = '/home/ubuntu/npcat/pdfs'

        for entry in os.listdir(root_dir):
            full_path = os.path.join(root_dir, entry)
            if os.path.isfile(full_path) and entry == file_path:
                message = MessageChain([
                    File(file_path)
                ])
                await msg.reply(rtf=message)

        option = jmcomic.create_option_by_file('option.yml')
        # 使用option对象来下载本子
        try:    
            jmcomic.download_album(jmcode, option)
        except Exception as e:
            await msg.reply('下载出问题了')

        message = MessageChain([
                    File(file_path)
                ])
        await msg.reply(rtf=message)


if __name__ == "__main__":
    bot.run(reload=False)